<style lang="less">
    @import 'login.less';
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                    <p class="login-tip">输入任意用户名和密码即可</p>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
import Cookies from 'js-cookie';
export default {
    data () {
        return {
            form: {
                userName: '',
                password: ''
            },
            rules: {
                userName: [
                    { required: true, message: '账号不能为空', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        handleSubmit () {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    Cookies.set('user', this.form.userName);
                    Cookies.set('password', this.form.password);
                    this.$ajax.post('/auth/login',{
                        userLoginName: this.form.userName,
                        password: this.form.password
                    }).then((response) => {
                        if (response.data.code === 0) {
                            console.log(response.data)
                            let {menus, token} = response.data.params
                            this.$bus.emit('menus', menus)
                            this.$bus.emit('token', token)
                            let count = this.$store.state.app.menuList.length
                            if (count === 0) {
                                this.$bus.alert("warning", "该用户没有任何功能权限", 3)
                                this.$bus.finish()
                            } else {
                                this.$store.state.app.loginUser = response.data.data
                                this.$store.state.app.pageOpenedList = []
                                this.$router.replace({name: 'home_index'});
                            }
                        } else {
                            this.$Message.error(response.data.msg);
                        }
                    }).then((error) => {
                        if (error) {
                            this.$Message.error("服务器调用异常,请联系管理员");
                        }
                    })
                }
            });
        }
    },
    created () {
        this.$bus.on('menus', (items) => {
            let menuList = []
            items.forEach((item, index) => {
                if (item.children.length === 1) {
                    menuList.push(item);
                } else {
                    let len = menuList.push(item);
                    let childrenArr = [];
                    childrenArr = item.children.filter(child => {
                        return child;
                    });
                    if (childrenArr === undefined || childrenArr.length === 0) {
                        menuList.splice(len - 1, 1);
                    } else {
                        let handledItem = JSON.parse(JSON.stringify(menuList[len - 1]));
                        handledItem.children = childrenArr;
                        menuList.splice(len - 1, 1, handledItem);
                    }
                }
            });
            this.$store.state.app.menuList = menuList
        })
        this.$bus.on('token', (token) => {
            this.$store.state.app.token = token
        })
    }
};
</script>

<style>

</style>
